﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using DataAccess;
using System.Transactions;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class PhieuTTNCC : MarshalByRefObject
    {

       static void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }

        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.PhieuTTNCC[] Select()
        {
            try
            {
                string sql = Common.Constants.PhieuTTNCC.SelectPhieuTTNCC;
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();

                while (dr.Read())
                {
                    Entities.PhieuTTNCC row = new Entities.PhieuTTNCC();

                    row.MaPhieuTTNCC = dr[Common.Constants.PhieuTTNCC.MaPhieuTTNCC].ToString();
                    row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuTTNCC.NgayLap].ToString());
                    row.MaNhaCungCap = dr[Common.Constants.PhieuTTNCC.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.PhieuTTNCC.TenNhaCungCap].ToString();
                    row.TongThanhToan = Common.Utilities.FormatMoney(dr[Common.Constants.PhieuTTNCC.TongThanhToan].ToString());
                    row.NoHienThoi = dr[Common.Constants.PhieuTTNCC.NoHienThoi].ToString();
                    row.Nguoinhan = dr[Common.Constants.PhieuTTNCC.Nguoinhan].ToString();
                    row.MaNhanVien = dr[Common.Constants.PhieuTTNCC.MaNhanVien].ToString();
                    row.MaKho = dr[Common.Constants.PhieuTTNCC.MaKho].ToString();
                    row.LoaiPhieu = bool.Parse(dr[Common.Constants.PhieuTTNCC.LoaiPhieu].ToString());
                    row.GhiChu = dr[Common.Constants.PhieuTTNCC.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.PhieuTTNCC.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.PhieuTTNCC[] arrC = new Entities.PhieuTTNCC[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.PhieuTTNCC)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;

            }
            catch 
            {
                return new Entities.PhieuTTNCC[0];
            }
          
      
        }

       /// <summary>
       /// select theo dia diem
       /// </summary>
       /// <param name="hdbh1"></param>
       /// <returns></returns>
        public static Entities.PhieuTTNCC[] SelectTheoDiaDiem(string MaKho)
       {
           try
           {
               string sql = Common.Constants.PhieuTTNCC.SelectPhieuTTNCCTheoDiaDiem;
               
               cn = DataAccess.Connection.OpenConnection();
               cmd = new SqlCommand(sql, cn);
               cmd.Parameters.Add(Common.Constants.PhieuChi.MaKho, SqlDbType.VarChar, 50).Value = MaKho;
               dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

               ArrayList arr = new ArrayList();

               while (dr.Read())
               {
                   Entities.PhieuTTNCC row = new Entities.PhieuTTNCC();

                   row.MaPhieuTTNCC = dr[Common.Constants.PhieuTTNCC.MaPhieuTTNCC].ToString();
                   row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuTTNCC.NgayLap].ToString());
                   row.MaNhaCungCap = dr[Common.Constants.PhieuTTNCC.MaNhaCungCap].ToString();
                   row.TenNhaCungCap = dr[Common.Constants.PhieuTTNCC.TenNhaCungCap].ToString();
                   row.TongThanhToan = Common.Utilities.FormatMoney(dr[Common.Constants.PhieuTTNCC.TongThanhToan].ToString());
                   row.NoHienThoi = dr[Common.Constants.PhieuTTNCC.NoHienThoi].ToString();
                   row.Nguoinhan = dr[Common.Constants.PhieuTTNCC.Nguoinhan].ToString();
                   row.MaNhanVien = dr[Common.Constants.PhieuTTNCC.MaNhanVien].ToString();
                   row.MaKho = dr[Common.Constants.PhieuTTNCC.MaKho].ToString();
                   row.LoaiPhieu = bool.Parse(dr[Common.Constants.PhieuTTNCC.LoaiPhieu].ToString());
                   row.GhiChu = dr[Common.Constants.PhieuTTNCC.GhiChu].ToString();
                   row.Deleted = bool.Parse(dr[Common.Constants.PhieuTTNCC.Deleted].ToString());
                   arr.Add(row);
               }
               int n = arr.Count;
               if (n == 0) return null;

               Entities.PhieuTTNCC[] arrC = new Entities.PhieuTTNCC[n];
               for (int i = 0; i < n; i++)
               {
                   arrC[i] = (Entities.PhieuTTNCC)arr[i];
               }

               //Giai phong bo nho
               arr = null;
               cmd.Connection.Dispose();
               cn.Close();
               cn = null;
               
               return arrC;

           }
           catch
           {
               return new Entities.PhieuTTNCC[0];
           }


       }
      
        /// <summary>
        /// insert
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool InsertUpdate(Entities.PhieuTTNCC phieuthanhtoannhacungcap)
        {
            TransactionScope ts = null;
            int i = 0; bool kt = false;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTNCC.InsertUpdatePhieuTTNCC;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.HanhDong, SqlDbType.NVarChar, 20).Value = phieuthanhtoannhacungcap.HanhDong;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.PhieuTTNCCID, SqlDbType.Int).Value = phieuthanhtoannhacungcap.PhieuTTNCCID;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.MaPhieuTTNCC, SqlDbType.VarChar, 50).Value = phieuthanhtoannhacungcap.MaPhieuTTNCC;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.NgayLap, SqlDbType.DateTime).Value = phieuthanhtoannhacungcap.NgayLap;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.MaNhaCungCap, SqlDbType.VarChar, 50).Value = phieuthanhtoannhacungcap.MaNhaCungCap;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.TenNhaCungCap, SqlDbType.NVarChar, 200).Value = phieuthanhtoannhacungcap.TenNhaCungCap;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.TongThanhToan, SqlDbType.Float).Value = phieuthanhtoannhacungcap.TongThanhToan;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.NoHienThoi, SqlDbType.Float).Value = phieuthanhtoannhacungcap.NoHienThoi;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.Nguoinhan, SqlDbType.NVarChar, 200).Value = phieuthanhtoannhacungcap.Nguoinhan;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.MaNhanVien, SqlDbType.VarChar, 50).Value = phieuthanhtoannhacungcap.MaNhanVien;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.MaKho, SqlDbType.VarChar, 50).Value = phieuthanhtoannhacungcap.MaKho;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.LoaiPhieu, SqlDbType.Bit).Value = phieuthanhtoannhacungcap.LoaiPhieu;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.GhiChu, SqlDbType.NVarChar, 200).Value = phieuthanhtoannhacungcap.GhiChu;
                    cmd.Parameters.Add(Common.Constants.PhieuTTNCC.Deleted, SqlDbType.Bit).Value = phieuthanhtoannhacungcap.Deleted;
                    i = cmd.ExecuteNonQuery();

                    if (i == 1)
                    {
                        kt = true;
                        if (phieuthanhtoannhacungcap.HanhDong == Common.Constants.Insert)
                        {
                            LuuViet
                                (
                                "Thêm",
                                phieuthanhtoannhacungcap.TaiKhoan.MaNhanVien,
                                "Thêm phiếu thanh toán nhà cung cấp " + phieuthanhtoannhacungcap.MaPhieuTTNCC,
                                phieuthanhtoannhacungcap.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                        else
                        {
                            LuuViet
                                (
                                "Sửa",
                                phieuthanhtoannhacungcap.TaiKhoan.MaNhanVien,
                                "Sửa phiếu thanh toán nhà cung cấp " + phieuthanhtoannhacungcap.MaPhieuTTNCC,
                                phieuthanhtoannhacungcap.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                    }
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;
                    //
                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool Delete(Entities.PhieuTTNCC ptdel)
        {
            try
            {
                bool kt = false;
                string sql = Common.Constants.PhieuTTNCC.DeletePhieuTTNCC;
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.PhieuTTNCC.HanhDong, SqlDbType.NVarChar, 20).Value = ptdel.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuTTNCC.MaPhieuTTNCC, SqlDbType.VarChar,50).Value = ptdel.MaPhieuTTNCC;
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    kt = true;
                    if (ptdel.HanhDong == Common.Constants.Insert)
                    {
                        LuuViet
                            (
                            "Xóa",
                            ptdel.TaiKhoan.MaNhanVien,
                            "Xóa phiếu thanh toán nhà cung cấp " + ptdel.MaPhieuTTNCC,
                            ptdel.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }



                }
                else
                    kt = false;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.HoaDonNhap[] SelectHDNhap()
        {
            try
            {
                string sql = Common.Constants.PhieuTTNCC.SelectHDNhap;
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HoaDonNhap row = new Entities.HoaDonNhap();

                    row.ThanhToanNgay = (dr[Common.Constants.HoaDonNhap.ThanhToanNgay].ToString());
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu].ToString();
                    row.TongTien = (dr[Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.HoaDonNhap[] arrC = new Entities.HoaDonNhap[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.HoaDonNhap)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch 
            {
                return new Entities.HoaDonNhap[0];
            }
          

        }

        /// <summary>
        /// update thanh toán su khi lập phiếu vào Hóa Đơn Nhập ( Loại phiếu là Phiếu Chi )
        /// </summary>
        /// <param name="mahdnhap"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdate(string mahdnhap, string thanhtoansaulapphieu)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTNCC.Update;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar, 50).Value = mahdnhap;
                    cmd.Parameters.Add(Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu, SqlDbType.VarChar, 50).Value = thanhtoansaulapphieu;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;
                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// update thanh toán su khi lập phiếu vào Trả lại nhà cung cấp ( Loại phiếu là Phiếu Thu )
        /// </summary>
        /// <param name="mahdnhap"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdateTraLaiNCC(string matralaincc, string thanhtoansaulapphieu, string Loai)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTNCC.UpdateTraLaiNCC;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.TraLaiNCC.MaHDTraLaiNCC, SqlDbType.VarChar, 50).Value = matralaincc;
                    cmd.Parameters.Add(Common.Constants.TraLaiNCC.ThanhToanSauKhiLapPhieu, SqlDbType.VarChar, 50).Value = thanhtoansaulapphieu;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        ///  Update Dư Nợ Vào Nhà Cung Cấp
        /// </summary>
        /// <param name="mahdnhap"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdateDuNoNCC(string mancc, string duno)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTNCC.UpdateDuNoNCC;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.NhaCungCap.MaNhaCungCap, SqlDbType.VarChar, 50).Value = mancc;
                    cmd.Parameters.Add(Common.Constants.NhaCungCap.NoHienThoi, SqlDbType.Float).Value = duno;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        public static Entities.HoaDonNhap[] SelectHDNhapTheoMaHDNhap(string maHDNhap)
        {
            Entities.HoaDonNhap[] list = null;
            try
            {
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.HoaDonNhap.sp_Select_HDNhapTheoMaHDNhap, cn);
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar, 50).Value = maHDNhap;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HoaDonNhap row = new Entities.HoaDonNhap();
                    row.MaHoaDonNhap = dr[Common.Constants.HoaDonNhap.MaHoaDonNhap].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.HoaDonNhap.NgayNhap].ToString());
                    row.MaNhaCungCap = dr[Common.Constants.HoaDonNhap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.HoaDonNhap.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.HoaDonNhap.NoHienThoi].ToString();
                    row.NguoiGiaoHang = dr[Common.Constants.HoaDonNhap.NguoiGiaoHang].ToString();
                    row.HinhThucThanhToan = dr[Common.Constants.HoaDonNhap.HinhThucThanhToan].ToString();
                    row.MaKho = dr[Common.Constants.HoaDonNhap.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.HoaDonNhap.TenKho].ToString();
                    row.HanThanhToan = DateTime.Parse(dr[Common.Constants.HoaDonNhap.HanThanhToan].ToString());
                    row.MaDonDatHang = dr[Common.Constants.HoaDonNhap.MaDonDatHang].ToString();
                    row.MaTienTe = dr[Common.Constants.HoaDonNhap.MaTienTe].ToString();
                    row.TyGiaTienTe = dr[Common.Constants.HoaDonNhap.TyGiaTienTe].ToString();
                    row.ChietKhau = dr[Common.Constants.HoaDonNhap.ChietKhau].ToString();
                    row.HinhThucThanhToan = dr[Common.Constants.HoaDonNhap.HinhThucThanhToan].ToString();
                    row.ThanhToanNgay = dr[Common.Constants.HoaDonNhap.ThanhToanNgay].ToString();
                    row.ThueGTGT = dr[Common.Constants.HoaDonNhap.ThueGTGT].ToString();
                    row.TongTien = dr[Common.Constants.HoaDonNhap.TongTien].ToString();
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu].ToString();
                    row.MaNhanVien = dr[Common.Constants.HoaDonNhap.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.HoaDonNhap.TenNhanVien].ToString();
                    row.GhiChu = dr[Common.Constants.HoaDonNhap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HoaDonNhap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.HoaDonNhap[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.HoaDonNhap)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
            return list;
        }

    }
}

#endregion